package com.liss.juc20211122.cf;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * FutureTask的局限性
 *  1.System.out.println(futureTask.get());//不见不散，只要出现get方法，不管是否计算完成都阻塞等待结果出来再运行
 *  2.System.out.println(futureTask.get(2L,TimeUnit.SECONDS));//过时不候
 *  3.不要阻塞，尽量用轮询替代，仍不是最理想
 */
public class FutureTaskDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        FutureTask<Integer> futureTask = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + "/t" + "----come in");
            try {
                TimeUnit.MILLISECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 1024;
        });
        new Thread(futureTask,"t1").start();

        //System.out.println(futureTask.get());

        //System.out.println(futureTask.get(2L,TimeUnit.SECONDS));

        while (true){
            if (futureTask.isDone()){
                System.out.println("----result: "+futureTask.get());
                break;
            } else {
                System.out.println("还在计算中，别催");
            }
        }
    }
}
